import { ref } from "vue";

export function usePlayAudio() {
  let lastPlayTime = 0;
  const PLAY_INTERVAL = 500;

  const playAudio = (english: string) => {
    if (english == "") {
      return;
    }
    const now = Date.now();
    if (now - lastPlayTime < PLAY_INTERVAL) return;
    lastPlayTime = now;
    const url =
      "https://dict.youdao.com/dictvoice?audio=" + english + "&type=1";
    const audio = ref(new Audio(url));
    audio.value.playbackRate = 1.0;
    audio.value.play();
  };

  return {
    playAudio,
  };
}
